home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Pascal Super Library
/
Pascal Super Library (CW International)(1997).bin
/
DELPHI32
/
COMPNENT
/
SAWIN95
/
CHKSTRED.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1996-10-11
|
14KB
|
493 lines
unit ChkStrEd;
interface
uses
{$IFDEF WIN32}
Windows,
{$ELSE}
WinTypes, WinProcs,
{$ENDIF}
SysUtils, Messages, Classes, Graphics, Controls,
Forms, StdCtrls, Buttons, IniFiles, DsgnIntf, Menus, Grids, Dialogs,
ExtCtrls;
type
TCheckStringListProperty = class( TPropertyEditor )
function GetAttributes : TPropertyAttributes; override;
function GetValue: string; override;
procedure Edit; override;
end;
TChkStrLstDlg = class( TForm )
PnlToolbar: TPanel;
BtnCut: TSpeedButton;
BtnCopy: TSpeedButton;
BtnPaste: TSpeedButton;
BtnUndo: TSpeedButton;
BtnFont: TSpeedButton;
PnlStringList: TPanel;
LblCount: TLabel;
Label1: TLabel;
LblLine: TLabel;
MnuEdit: TPopupMenu;
MnuUndo: TMenuItem;
MnuCut: TMenuItem;
MnuCopy: TMenuItem;
MnuPaste: TMenuItem;
DlgFont: TFontDialog;
BtnOk: TButton;
BtnCancel: TButton;
CHKDefault: TCheckBox;
grdEdit: TStringGrid;
cmdDelete: TSpeedButton;
cmdDown: TSpeedButton;
cmdUp: TSpeedButton;
EdtStrings: TMemo;
procedure FormCreate( Sender : TObject );
procedure FormDestroy( Sender : TObject );
procedure BtnOkClick( Sender : TObject );
procedure BtnFontClick( Sender : TObject );
procedure BtnUndoClick( Sender : TObject );
procedure BtnCutClick( Sender : TObject );
procedure BtnCopyClick( Sender : TObject );
procedure BtnPasteClick( Sender : TObject );
procedure grdEditClick(Sender: TObject);
procedure grdEditDrawCell(Sender: TObject; Col, Row: Longint;
Rect: TRect; State: TGridDrawState);
procedure grdEditDblClick(Sender: TObject);
procedure grdEditKeyPress(Sender: TObject; var Key: Char);
procedure grdEditMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure grdEditMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure cmdDeleteClick(Sender: TObject);
procedure cmdDownClick(Sender: TObject);
procedure cmdUpClick(Sender: TObject);
procedure grdEditKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
DelphiIni : TIniFile;
FPropName : string;
procedure UpdateLineColStatus;
procedure UpdateClipboardStatus;
procedure EnableButtons( Enable : Boolean );
procedure ResizeGrid;
public
procedure SetTextToGrid;
procedure SetGridToText;
end;
implementation
{$R *.DFM}
uses
ClipBrd, Printers, ChkList;
const
Section = 'Nedap.ChkStrListEditor';
fsBoldMask = 8;
fsItalicMask = 4;
fsUnderlineMask = 2;
fsStrikeOutMask = 1;
fsNormal = 0;
{ TCheckStringListProperty Methods }
function TCheckStringListProperty.GetAttributes: TPropertyAttributes;
begin
Result := [ paReadOnly, paDialog ]; { Edit method will display a dialog }
end;
function TCheckStringListProperty.GetValue : string;
begin
{ The GetPropType method is used to retrieve information pertaining to the }
{ property type being edited. In this case, the Name of the property class }
{ is displayed in the value column of the Object Inspector. }
Result := Format( '(%s)', [ GetPropType^.Name ] );
end;
procedure TCheckStringListProperty.Edit;
var
Dialog : TChkStrLstDlg;
Cls : TCheckListStrings;
i : Integer;
begin
Dialog := TChkStrLstDlg.Create( Application );
try
if PropCount = 1 then
begin
Dialog.FPropName := GetComponent(0).Owner.Name + '.' +
GetComponent(0).Name + '.' + GetName;
Dialog.Caption := Dialog.FPropName + ' - ' + Dialog.Caption;
end;
{ Copy string list of property into the memo of the dialog }
Cls := TCheckListStrings(GetOrdValue);
for i:=0 to Cls.Count-1 do
case Cls.State[i] of
csUnchecked: Dialog.EdtStrings.Lines.Add('0|'+Cls.Strings[i]);
csChecked : Dialog.EdtStrings.Lines.Add('1|'+Cls.Strings[i]);
csGrayed : Dialog.EdtStrings.Lines.Add('2|'+Cls.Strings[i]);
end;
Dialog.SetTextToGrid;
Dialog.UpdateLineColStatus;
if Dialog.ShowModal = mrOK then
begin
Cls := TCheckListStrings.Create;
SetOrdValue( Longint( Dialog.EdtStrings.Lines ) );
Cls.Free;
end;
finally
Dialog.Free;
end;
end;
type
TNewStrGrid = class(TStringGrid); { Needed to access InPlaceEditor }
{ TChkStrLstDlg Methods }
procedure TChkStrLstDlg.SetTextToGrid;
var
sText : string;
i : integer;
begin
grdEdit.RowCount := edtStrings.Lines.Count+2;
for i:=0 to edtStrings.Lines.Count-1 do
begin
sText := edtStrings.Lines[i];
grdEdit.Cells[0, i+1] := Copy(sText, 1, 1);
grdEdit.Cells[1, i+1] := Copy(sText, 3, Length(sText));
end;
grdEdit.Cells[0, grdEdit.RowCount-1] := '0';
ResizeGrid;
end;
procedure TChkStrLstDlg.SetGridToText;
var
i : Integer;
begin
edtStrings.Clear;
for i:=1 to grdEdit.RowCount-2 do
edtStrings.Lines.Add(grdEdit.Cells[0, i]+'|'+grdEdit.Cells[1, i]);
end;
procedure TChkStrLstDlg.ResizeGrid;
begin
with grdEdit do
if VisibleRowCount<RowCount-1 then
ColWidths[1] := Width - ColWidths[0] - 6 - GetSystemMetrics(SM_CYHSCROLL)
else
ColWidths[1] := Width - ColWidths[0] - 6;
end;
procedure TChkStrLstDlg.FormCreate(Sender: TObject);
var
StyleBits : Byte;
begin
{ Load settings from DELPHI.INI File }
DelphiIni := TIniFile.Create( 'DELPHI.INI' );
with grdEdit.Font do
begin
Name := DelphiIni.ReadString( Section, 'FontName', 'MS Sans Serif' );
Size := DelphiIni.ReadInteger( Section, 'FontSize', 8 );
Color := DelphiIni.ReadInteger( Section, 'FontColor', clBlack );
StyleBits := DelphiIni.ReadInteger( Section, 'FontStyle', fsNormal );
Style := [];
if StyleBits and fsBoldMask = fsBoldMask then
Style := Style + [ fsBold ];
if StyleBits and fsItalicMask = fsItalicMask then
Style := Style + [ fsItalic ];
if StyleBits and fsUnderlineMask = fsUnderlineMask then
Style := Style + [ fsUnderline ];
if StyleBits and fsStrikeOutMask = fsStrikeOutMask then
Style := Style + [ fsStrikeOut ];
end;
grdEdit.Cells[0,0] := 'State';
grdEdit.Cells[1,0] := 'Text';
end;
procedure TChkStrLstDlg.FormDestroy(Sender: TObject);
begin
DelphiIni.Free;
end;
procedure TChkStrLstDlg.BtnOkClick(Sender: TObject);
var
StyleBits : Byte;
begin
if ChkDefault.Checked then
begin { Save New Default Settings }
with grdEdit.Font do
begin
DelphiIni.WriteString( Section, 'FontName', Name );
DelphiIni.WriteInteger( Section, 'FontSize', Size );
DelphiIni.WriteInteger( Section, 'FontColor', Color );
StyleBits := 0;
if fsBold in Style then
StyleBits := fsBoldMask;
if fsItalic in Style then
StyleBits := StyleBits + fsItalicMask;
if fsUnderline in Style then
StyleBits := StyleBits + fsUnderlineMask;
if fsStrikeOut in Style then
StyleBits := StyleBits + fsStrikeOutMask;
DelphiIni.WriteInteger( Section, 'FontStyle', StyleBits );
end;
end;
SetGridToText;
end;
procedure TChkStrLstDlg.BtnFontClick(Sender: TObject);
begin
DlgFont.Font := grdEdit.Font;
if DlgFont.Execute then
begin
grdEdit.Font := DlgFont.Font; { Assign new font to Memo field }
end;
end;
procedure TChkStrLstDlg.BtnUndoClick(Sender: TObject);
begin
TNewStrGrid(grdEdit).InPlaceEditor.Perform( wm_Undo, 0, 0 );
end;
procedure TChkStrLstDlg.BtnCutClick(Sender: TObject);
begin
TNewStrGrid(grdEdit).InplaceEditor.CutToClipboard;
UpdateClipboardStatus;
end;
procedure TChkStrLstDlg.BtnCopyClick(Sender: TObject);
begin
TNewStrGrid(grdEdit).InplaceEditor.CopyToClipboard;
UpdateClipboardStatus;
end;
procedure TChkStrLstDlg.BtnPasteClick(Sender: TObject);
begin
TNewStrGrid(grdEdit).InplaceEditor.PasteFromClipboard;
end;
procedure TChkStrLstDlg.UpdateLineColStatus;
begin
LblLine.Caption := IntToStr( grdEdit.Row );
LblCount.Caption := IntToStr( grdEdit.RowCount-1) + ' Line(s)';
UpdateClipboardStatus;
end;
procedure TChkStrLstDlg.UpdateClipboardStatus;
var
HasText : Boolean;
HasSelection : Boolean;
begin
if TNewStrGrid(grdEdit).InplaceEditor<>nil then
HasSelection := TNewStrGrid(grdEdit).InPlaceEditor.SelLength <> 0
else
HasSelection := False;
BtnCut.Enabled := HasSelection; { Cut and Copy are only enabled if }
MnuCut.Enabled := HasSelection; { the user has selected some text }
BtnCopy.Enabled := HasSelection;
MnuCopy.Enabled := HasSelection;
HasText := Clipboard.HasFormat( cf_Text );
BtnPaste.Enabled := HasText; { Paste is only enabled if the }
MnuPaste.Enabled := HasText; { Clipboard contains Text }
end;
procedure TChkStrLstDlg.EnableButtons( Enable : Boolean );
var
SysMenu : HMenu;
begin
BtnUndo.Enabled := Enable;
BtnFont.Enabled := Enable;
BtnOK.Enabled := Enable;
BtnCancel.Enabled := Enable;
ChkDefault.Enabled := Enable;
EdtStrings.Enabled := Enable;
BtnCut.Enabled := Enable;
BtnCopy.Enabled := Enable;
BtnPaste.Enabled := Enable;
if Enable then
UpdateClipboardStatus;
{ Disable the Close menu item, so dialog cannot be closed }
SysMenu := GetSystemMenu( Handle, False );
if Enable then
EnableMenuItem( SysMenu, sc_Close, mf_ByCommand or mf_Enabled )
else
EnableMenuItem( SysMenu, sc_Close, mf_ByCommand or mf_Disabled or mf_Grayed );
end;
procedure TChkStrLstDlg.grdEditClick(Sender: TObject);
begin
with grdEdit do
begin
if Col=1 then
begin
Options := Options + [goEditing];
EditorMode := True;
end
else
Options := Options - [goEditing];
end;
UpdateLineColStatus;
end;
procedure TChkStrLstDlg.grdEditDrawCell(Sender: TObject; Col, Row: Longint;
Rect: TRect; State: TGridDrawState);
var
FMinWidth : Integer;
RectCheck : TRect;
Halfy, i : Integer;
begin
if (Col=0) and (Row>0) then
begin
FMinWidth := 13;
CopyRect(RectCheck, Rect);
RectCheck.left := ((Rect.right-Rect.left) - FMinWidth) div 2;
RectCheck.top := Rect.top + ((Rect.bottom-Rect.top) - FMinWidth) div 2;
RectCheck.bottom := RectCheck.top + FMinWidth;
RectCheck.right := RectCheck.left + FMinWidth;
with grdEdit.Canvas, RectCheck do
begin
FillRect(Rect);
Pen.Color := clBtnShadow;
if grdEdit.Cells[Col, Row]='2' then
Brush.Color := clBtnFace
else
Brush.Color := clWindow;
Rectangle(left+1, top+1, right-1, bottom-1);
if (grdEdit.Cells[Col, Row]='1') or (grdEdit.Cells[Col, Row]='2') then
begin
InflateRect(RectCheck, -3, -3);
Pen.Color := clBlack;
Pen.Width := 1;
halfy := top+(bottom-top) div 2 + 1;
for i:=0 to 2 do
begin
PolyLine([Point(left,halfy-i), Point(left+2, halfy+2-i)]);
PolyLine([Point(left+2, halfy+2-i), Point(left+7, halfy-3-i)]);
end;
end
end
end;
end;
procedure TChkStrLstDlg.grdEditDblClick(Sender: TObject);
begin
if grdEdit.Col=0 then
case grdEdit.Cells[0, grdEdit.Row][1] of
'0' : grdEdit.Cells[0, grdEdit.Row] := '1';
'1' : grdEdit.Cells[0, grdEdit.Row] := '2';
'2' : grdEdit.Cells[0, grdEdit.Row] := '0';
end;
end;
procedure TChkStrLstDlg.grdEditKeyPress(Sender: TObject; var Key: Char);
begin
if grdEdit.Col=0 then
begin
Key := #0;
Exit;
end;
if grdEdit.Row=grdEdit.RowCount-1 then
begin
grdEdit.RowCount := grdEdit.RowCount+1;
grdEdit.Cells[0, grdEdit.RowCount-1] := '0';
UpdateLineColStatus;
ResizeGrid;
end;
end;
procedure TChkStrLstDlg.grdEditMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
UpdateClipboardStatus;
end;
procedure TChkStrLstDlg.grdEditMouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
begin
UpdateClipboardStatus;
end;
procedure TChkStrLstDlg.cmdDeleteClick(Sender: TObject);
var
i : integer;
begin
if grdEdit.RowCount>2 then
begin
for i:=grdEdit.Row to grdEdit.RowCount-1 do
begin
grdEdit.Cells[0, i] := grdEdit.Cells[0, i+1];
grdEdit.Cells[1, i] := grdEdit.Cells[1, i+1];
end;
grdEdit.RowCount := grdEdit.RowCount-1;
end
else
i:=1;
grdEdit.Cells[0, grdEdit.RowCount-1] := '0';
grdEdit.Cells[1, grdEdit.RowCount-1] := '';
UpdateLineColStatus;
end;
procedure TChkStrLstDlg.cmdDownClick(Sender: TObject);
var
sTmp : array[1..2] of string;
iRow : integer;
begin
iRow := grdEdit.Row;
if iRow<grdEdit.RowCount-1 then
begin
sTmp[1] := grdEdit.Cells[0, iRow];
sTmp[2] := grdEdit.Cells[1, iRow];
grdEdit.Cells[0, iRow] := grdEdit.Cells[0, iRow+1];
grdEdit.Cells[1, iRow] := grdEdit.Cells[1, iRow+1];
grdEdit.Cells[0, iRow+1] := sTmp[1];
grdEdit.Cells[1, iRow+1] := sTmp[2];
grdEdit.Row := iRow+1;
UpdateLineColStatus;
end;
end;
procedure TChkStrLstDlg.cmdUpClick(Sender: TObject);
var
sTmp : array[1..2] of string;
iRow : integer;
begin
iRow := grdEdit.Row;
if iRow>1 then
begin
sTmp[1] := grdEdit.Cells[0, iRow];
sTmp[2] := grdEdit.Cells[1, iRow];
grdEdit.Cells[0, iRow] := grdEdit.Cells[0, iRow-1];
grdEdit.Cells[1, iRow] := grdEdit.Cells[1, iRow-1];
grdEdit.Cells[0, iRow-1] := sTmp[1];
grdEdit.Cells[1, iRow-1] := sTmp[2];
grdEdit.Row := iRow-1;
UpdateLineColStatus;
end;
end;
procedure TChkStrLstDlg.grdEditKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if grdEdit.Col=0 then Key:=0;
end;
end.